package _8buildin_types

import "fmt"

func Slice() {
	s1 := []int{9, 8, 7}
	fmt.Printf("s1: %v, len=%d, cap=%d \n", s1, len(s1), cap(s1))

	s2 := make([]int, 3, 4)
	fmt.Printf("s2: %v, len=%d, cap=%d \n", s2, len(s2), cap(s2))

	s3 := make([]int, 4)
	fmt.Printf("s3: %v, len=%d, cap=%d \n", s3, len(s3), cap(s3))

	s4 := make([]int, 0, 4)
	s4 = append(s4, 1)
	s4 = append(s4, 2)
	fmt.Printf("s4: %v, len=%d, cap=%d \n", s4, len(s4), cap(s4))

}

func SubSlice() {
	s1 := []int{2, 4, 6, 8, 10}
	s2 := s1[1:3]
	fmt.Printf("s2: %v, len:%d, cap:%d \n", s2, len(s2), cap(s2))

	// 子切片的容量就是从start开始往后，包括原本s1的底层数组的元素个数
	s3 := s1[2:]
	fmt.Printf("s3: %v, len:%d, cap:%d \n", s3, len(s3), cap(s3))
	s4 := s1[:3]
	fmt.Printf("s4: %v, len:%d, cap:%d \n", s4, len(s4), cap(s4))
}

func ShareSlice() {
	s1 := []int{1, 2, 3, 4}
	s2 := s1[2:]
	fmt.Printf("s2: %v, len=%d, cap=%d \n", s2, len(s2), cap(s2))

	// 子切片没有扩容，子切片与原切片共享内存，此时修改子切片的内容会影响原切片的内容
	s2[0] = 99
	fmt.Printf("s2: %v, len=%d, cap=%d, s2的地址：%p \n", s2, len(s2), cap(s2), &s2)
	//fmt.Printf("s2第三个元素的地址：%p \n", &s2[2])
	fmt.Printf("s1: %v, len=%d, cap=%d \n", s1, len(s1), cap(s1))

	// 如果子切片发生了扩容，则子切片与原切片不共享内存，此时修改子切片的内容不影响原切片的内容
	s2 = append(s2, 199)
	fmt.Printf("s2: %v, len=%d, cap=%d, s2的地址：%p \n", s2, len(s2), cap(s2), &s2)
	fmt.Printf("s1: %v, len=%d, cap=%d \n", s1, len(s1), cap(s1))

	s2 = append(s2, 299)
	fmt.Printf("s2: %v, len=%d, cap=%d, s2的地址：%p \n", s2, len(s2), cap(s2), &s2)

	s2 = append(s2, 399)
	fmt.Printf("s2: %v, len=%d, cap=%d, s2的地址：%p \n", s2, len(s2), cap(s2), &s2)

	//s3 := s1[0:2]
	//fmt.Printf("s3: %v, len=%d, cap=%d \n", s3, len(s3), cap(s3))
	//s3 = append(s3, 100)
	//fmt.Printf("s3: %v, len=%d, cap=%d \n", s3, len(s3), cap(s3))
	//fmt.Printf("s1: %v, len=%d, cap=%d \n", s1, len(s1), cap(s1))
}
